﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;
using MINERP.MODEL.Frontier;
using MINERP.COM;
using MINERP.DAL.Frontier;


namespace MINERP.BLL.Frontier
{
    public class OrderInfoBLL
    {
        //得到订单列表
        public List<OrderInfo_Info> GetOrderInfoList(int printerId,int status)
        {
            DateTime doday = DateTime.Now.Date;
            DateTime nextDay = DateTime.Now.AddDays(1).Date;
            List<OrderInfo_Info> list = new List<OrderInfo_Info>();
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                //取出未打印状态的orderfile
                List<OrderFile> orderFileList = null ;
                switch (status)
                {
                    case 0://未打印
                        orderFileList = enty.OrderFile.Where(o => o.Status == (int)OrderFileStatus.Created && o.printer.PrinterID == printerId).OrderByDescending(o => o.Priority).ThenBy(o => o.createTime).ToList();
                        break;
                    case 1://今日打印完成
                        orderFileList = enty.OrderFile.Where(o =>o.Status == (int)OrderFileStatus.Printed && o.printer.PrinterID == printerId && o.lastPrintTime >= doday && o.lastPrintTime<nextDay).OrderByDescending(o => o.Priority).ThenBy(o => o.createTime).ToList();
                        break;
                    case 2://生成错误
                        orderFileList = enty.OrderFile.Where(o => o.Status == (int)OrderFileStatus.CreateErr && o.printer.PrinterID == printerId).OrderByDescending(o => o.Priority).ThenBy(o => o.createTime).ToList();
                        break;
                    default :
                        break;
                }
                
                List<int> orderProductIdList = new List<int>();

                foreach (var item in orderFileList)
                {
                    if (!item.orderInfoReference.IsLoaded)
                    {
                        item.orderInfoReference.Load();
                    }
                  
                    orderInfo oi = item.orderInfo;
                    if (!orderProductIdList.Contains(oi.OrderProductId))
                    {
                        orderProductIdList.Add(oi.OrderProductId);
                    }

                }

                foreach (var item in orderProductIdList)
                {
                    orderInfo orderInfo = enty.orderInfo.FirstOrDefault(o => o.OrderProductId == item);
                    if(!orderInfo.Tb_OrderBagReference.IsLoaded)
                    {
                        orderInfo.Tb_OrderBagReference.Load();
                    }
                   
                    if (!orderInfo.OrderFile.IsLoaded)
                    {
                        orderInfo.OrderFile.Load();
                    }
                    foreach (var orderfile in orderInfo.OrderFile)
                    {
                        if (!orderfile.paperTypeReference.IsLoaded)
                        {
                            orderfile.paperTypeReference.Load();
                        }
                        if (!orderInfo.photoInfo.IsLoaded)
                        {
                            orderInfo.photoInfo.Load();
                        }
                    }
                    string [] bagNameList = orderInfo.Tb_OrderBag.BagName.Split('_');
                    string bagName = bagNameList[0];
                    if (status == 0)
                    {
                        bool allOrderFileNoPrinted = true;//产品中没有一个文件夹是错误或修正状态

                        foreach (var orderfile in orderInfo.OrderFile)
                        {
                            if (orderfile.Status != (int)OrderFileStatus.Created&&orderfile.Status !=(int)OrderFileStatus.Printed)
                            {
                                allOrderFileNoPrinted = false;
                            }
                            if (!orderfile.paperTypeReference.IsLoaded)
                            {
                                orderfile.paperTypeReference.Load();
                            }
                            if (!orderfile.paperFaceReference.IsLoaded)
                            {
                                orderfile.paperFaceReference.Load();
                            }
                        }
                        
                        if (!allOrderFileNoPrinted)
                        {
                            continue;
                        }

                        OrderInfo_Info oi_Info = MappingOrderInfo(orderInfo,status,bagName);
                       
                        list.Add(oi_Info);
                    }
                    if (status == 1)
                    {

                        bool allOrderFilePrinted = true;//所有的文件夹都是打印完成状态
                        foreach (var orderfile in orderInfo.OrderFile)
                        {
                            if (orderfile.Status != (int)OrderFileStatus.Printed)
                            {
                                allOrderFilePrinted = false;
                            }
                            if (!orderfile.paperTypeReference.IsLoaded)
                            {
                                orderfile.paperTypeReference.Load();
                            }
                            if (!orderfile.paperFaceReference.IsLoaded)
                            {
                                orderfile.paperFaceReference.Load();
                            }
                        }
                        if (!allOrderFilePrinted)
                        {
                            continue;
                        }

                        OrderInfo_Info oi_Info = MappingOrderInfo(orderInfo,status,bagName);
                       
                        list.Add(oi_Info);
 
                    }
                    if (status == 2)
                    {

                        OrderInfo_Info oi_Info = MappingOrderInfo(orderInfo, status, bagName);
                     
                        list.Add(oi_Info);

                    }
                }

            }
            return list;
        }

        private OrderInfo_Info MappingOrderInfo(orderInfo orderInfo, int status,string bagName)
        {
            int photoNum = 0;//一个产品的照片总数
            OrderInfo_Info oi_Info = new OrderInfo_Info();
            oi_Info.IsCheck = false;
            oi_Info.CreateTime = orderInfo.OrderFile.First().createTime;
            oi_Info.CtmName = orderInfo.CtmName;
            oi_Info.OrderProductId = orderInfo.OrderProductId;
            oi_Info.OrderProductName = orderInfo.OrderProductName;
            switch (status)
            {
                case 0:
                    oi_Info.StatusName = "未打印";
                    break;
                case 1:
                    oi_Info.StatusName = "打印完成";
                    break;
                case 2:
                    oi_Info.StatusName = "生成错误";
                    break;
            }
            oi_Info.Width = orderInfo.Width;
            oi_Info.Length = orderInfo.Height;
            oi_Info.Priority = orderInfo.OrderFile.First().Priority;
            oi_Info.PaperTypeName = orderInfo.OrderFile.First().paperType.Name;
            //int paperFaceId = orderInfo.OrderFile.First().paperFace.PaperFaceID;
            //oi_Info.PaperFaceName = enty.paperFace.FirstOrDefault(p => p.PaperFaceID == paperFaceId).Name;
           
            oi_Info.Remark = orderInfo.Remark;
            oi_Info.BagName = bagName;

            foreach (var item in orderInfo.OrderFile)
            {
                if (!item.photoInfo.IsLoaded)
                {
                    item.photoInfo.Load();
                }
                foreach (var pho in item.photoInfo)
                {
                    photoNum += (int)pho.TotalNum;
                }
            }
            oi_Info.PhotoNum = orderInfo.productNum * photoNum;

            return oi_Info;

        }

        /// <summary>
        /// 根据查询条件得到订单列表
        /// </summary>
        /// <param name="printerId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<OrderInfo_Info> GetOrderInfoListByCondition(int printerId,string ctmName,string productName,DateTime startTime,DateTime endTime ,int orderProductId)
        {
            List<OrderInfo_Info> list = new List<OrderInfo_Info>();
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                List<int> orderProductIdList = ProntierDAL.GetListOrderProductId(printerId, ctmName, productName, startTime, endTime, orderProductId);

                foreach (var item in orderProductIdList)
                {
                    
                    orderInfo orderInfo = enty.orderInfo.FirstOrDefault(o => o.OrderProductId == item);

                    if(!orderInfo.Tb_OrderBagReference.IsLoaded)
                    {
                        orderInfo.Tb_OrderBagReference.Load();
                    }
                   
                    if (!orderInfo.OrderFile.IsLoaded)
                    {
                        orderInfo.OrderFile.Load();
                    }
                    foreach (var orderfile in orderInfo.OrderFile)
                    {
                        if (!orderfile.paperTypeReference.IsLoaded)
                        {
                            orderfile.paperTypeReference.Load();
                        }
                        if (!orderInfo.photoInfo.IsLoaded)
                        {
                            orderInfo.photoInfo.Load();
                        }
                    }
                    string [] bagNameList = orderInfo.Tb_OrderBag.BagName.Split('_');
                    string bagName = bagNameList[0];

                    bool allfilePrinted = true;

                    foreach (var orderfile in orderInfo.OrderFile)
                    {
                        if (orderfile.Status != (int)OrderFileStatus.Printed)
                        {
                            allfilePrinted = false;
                        }
                    }
                    if (!allfilePrinted)
                        continue;
                    //OrderInfo_Info oi_Info = new OrderInfo_Info();
                    //oi_Info.IsCheck = false;
                    //oi_Info.CreateTime = orderInfo.OrderFile.First().createTime;
                    //oi_Info.CtmName = orderInfo.CtmName;
                    //oi_Info.OrderProductId = orderInfo.OrderProductId;
                    //oi_Info.OrderProductName = orderInfo.OrderProductName;
                    //oi_Info.StatusName = "打印完成";
                    //oi_Info.Width = orderInfo.Width;
                    //oi_Info.Length = orderInfo.Height;
                    //oi_Info.Priority = orderInfo.OrderFile.First().Priority;
                    //int paperTypeId = orderInfo.OrderFile.First().paperType.PaperTypeID;
                    //oi_Info.PaperTypeName = enty.paperType.FirstOrDefault(p => p.PaperTypeID == paperTypeId).Name;
                    ////int paperFaceId = orderInfo.OrderFile.First().paperFace.PaperFaceID;
                    ////oi_Info.PaperFaceName = enty.paperFace.FirstOrDefault(p => p.PaperFaceID == paperFaceId).Name;
                    //oi_Info.Remark = orderInfo.Remark;
                    OrderInfo_Info oi_Info = MappingOrderInfo(orderInfo,1, bagName);
                    list.Add(oi_Info);

                    
                }

            }
            return list;
        }
    }
}
